import cv2
def face3Data():
    name=1
    #调用本地摄像头，0允许被调用
    video=cv2.VideoCapture(0)
    faceData=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
    #获取人脸
    while(True):
        flag,videoImg=video.read()
        #镜像翻转 1水平翻转 0垂直翻转 -1水平+垂直
        videoImg=cv2.flip(videoImg,1)
        #对比人脸
        faces=faceData.detectMultiScale(videoImg)
        # print(faces)
        for x,y,w,h in faces:
            cv2.rectangle(videoImg,pt1=(x,y),pt2=(x+w,y+h),color=[0,255,0],thickness=3)
            if not isinstance(faces,tuple):
                facePhoto=videoImg[y:y+h,x:x+w]
                #将人脸切片加入文件夹
                cv2.imwrite("ren/2/%s.jpg"%name,facePhoto)
                print("第%s张人脸数据保存成功"%name)
                name+=1
        cv2.imshow("YU",videoImg)
        # cv2.waitKey(1000//24)
        index=cv2.waitKey(1000//24)
        #点击空格，ASCII值32
        if index==32:
            print("窗口即将关闭......")
            break
    cv2.destroyWindow()
